package com.apkicon.processor;

import com.apkicon.model.IconInfo;
import com.apkicon.parser.ApkParserLibrary;
import com.apkicon.parser.StandardAdaptiveIconParser;
import net.dongliu.apk.parser.ApkFile;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.ByteArrayInputStream;

/**
 * Enhanced Adaptive Icon处理器
 * 使用EnhancedAdaptiveIconProcessor来提供更真实的渲染效果
 */
public class AdaptiveIconProcessor implements IconProcessor {
    
    @Override
    public IconInfo processIcon(ApkParserLibrary.ApkInfo apkInfo, ApkParserLibrary.IconResource iconResource, int targetSize) throws Exception {
        try (ApkFile apk = new ApkFile(apkInfo.getApkFile())) {
            System.out.println("🔍 Processing Adaptive Icon: " + iconResource.getPath());
            
            // 使用标准Adaptive Icon解析器获取资源路径
            StandardAdaptiveIconParser.AdaptiveIconResult result = 
                StandardAdaptiveIconParser.parseAdaptiveIcon(apk, iconResource.getPath());
            
            System.out.println("📱 Parsing Result:");
            System.out.println("   Background: " + result.getBackgroundPath());
            System.out.println("   Foreground: " + result.getForegroundPath());
            System.out.println("   Monochrome: " + result.getMonochromePath());
            
            // 使用增强的渲染算法
            BufferedImage iconImage = renderWithEnhancedAlgorithm(apk, result, targetSize);
            
            if (iconImage == null) {
                throw new IllegalArgumentException("Failed to render Adaptive Icon: " + iconResource.getPath());
            }
            
            System.out.println("✅ Enhanced Adaptive Icon rendering completed (" + targetSize + "x" + targetSize + ")");
            
            return new IconInfo(iconImage, IconInfo.IconType.ADAPTIVE_ICON, iconResource.getPath());
        }
    }
    
    /**
     * 使用增强算法渲染Adaptive Icon
     */
    private BufferedImage renderWithEnhancedAlgorithm(ApkFile apk, StandardAdaptiveIconParser.AdaptiveIconResult result, int targetSize) throws Exception {
        // 加载背景图片
        BufferedImage backgroundImage = null;
        if (result.getBackgroundPath() != null) {
            byte[] backgroundData = apk.getFileData(result.getBackgroundPath());
            if (backgroundData != null) {
                backgroundImage = ImageIO.read(new ByteArrayInputStream(backgroundData));
                System.out.println("✅ Loaded background: " + result.getBackgroundPath() + 
                                 " (" + backgroundImage.getWidth() + "x" + backgroundImage.getHeight() + ")");
            }
        }
        
        // 加载前景图片
        BufferedImage foregroundImage = null;
        if (result.getForegroundPath() != null) {
            byte[] foregroundData = apk.getFileData(result.getForegroundPath());
            if (foregroundData != null) {
                foregroundImage = ImageIO.read(new ByteArrayInputStream(foregroundData));
                System.out.println("✅ Loaded foreground: " + result.getForegroundPath() + 
                                 " (" + foregroundImage.getWidth() + "x" + foregroundImage.getHeight() + ")");
            }
        }
        
        // 使用增强处理器处理图片
        BufferedImage processedBackground = EnhancedAdaptiveIconProcessor.processBackgroundImage(backgroundImage);
        BufferedImage processedForeground = EnhancedAdaptiveIconProcessor.processForegroundImage(foregroundImage);
        
        // 使用增强算法渲染
        return EnhancedAdaptiveIconProcessor.renderAdaptiveIcon(processedBackground, processedForeground, targetSize);
    }
}